//
//  UFOSXPHeroTimePageCollectionViewCell.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOSXPHeroTimePageCollectionViewCell.h"

#import "UFOHeroTimeHotPageCollectionViewCell.h"
#import "UFOHeroTimeNewPageCollectionViewCell.h"

#import "UFOSXPHeroTimeHotPageListModel.h"
#import "UFOSXPHeroTimeNewPageListModel.h"

#define SPACE 10

@interface UFOSXPHeroTimePageCollectionViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *hotCollectionV;
@property (nonatomic, retain) UICollectionView *NewCollectionV;
@end
@implementation UFOSXPHeroTimePageCollectionViewCell

- (void)dealloc
{
    [_hotDataArr release];
    [_NewDataArr release];
    [_hotCollectionV release];
    [_NewCollectionV release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

// 重写self.hotDataArr的setter方法
- (void)setHotDataArr:(NSMutableArray *)hotDataArr
{
    if (_hotDataArr != hotDataArr) {
        [_hotDataArr release];
        _hotDataArr = [hotDataArr retain];
    }
    [self.hotCollectionV reloadData];
    [self.hotCollectionV.mj_footer endRefreshing];
}

// 重写self.NewDataArr的setter方法
- (void)setNewDataArr:(NSMutableArray *)NewDataArr
{
    if (_NewDataArr != NewDataArr) {
        [_NewDataArr release];
        _NewDataArr = [NewDataArr retain];
    }
    [self.NewCollectionV reloadData];
}

// 创建视图
- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];
    [self createSegmentC];
    [self createHotCollectView];
    [self createNewCollectionView];
}

// 创建segment
- (void)createSegmentC
{
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"最热", @"最新", nil]];
    segmentC.frame = CGRectMake(0, 0, SCREENWIDTH, 40 * SCREENHEIGHT / HEIGHTSIXP);
    segmentC.selectedSegmentIndex = 0;
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0.249 green:0.609 blue:1.000 alpha:1.000],NSForegroundColorAttributeName,[UIFont systemFontOfSize:14.0 * SCREENHEIGHT / HEIGHTSIXP],NSFontAttributeName ,nil];
    
    //设置各种状态的字体和颜色
    [segmentC setTitleTextAttributes:dic forState:UIControlStateNormal];
    segmentC.tintColor = [UIColor colorWithRed:0.249 green:0.609 blue:1.000 alpha:1.000];
    [segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.contentView addSubview:segmentC];
    [segmentC release];
}

// 创建最热collectView
- (void)createHotCollectView
{
    UICollectionViewFlowLayout *flowL = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowL.minimumLineSpacing = SPACE * SCREENHEIGHT / HEIGHTSIXP;
    flowL.minimumInteritemSpacing = SPACE * SCREENWIDTH / WIDTHSIXP;
    flowL.headerReferenceSize = CGSizeMake(0, 0);
    flowL.footerReferenceSize = CGSizeMake(0, 0);
    
    flowL.itemSize = CGSizeMake(((SCREENWIDTH - SPACE * 3 * SCREENWIDTH / WIDTHSIXP) / 2.0), ((SCREENWIDTH - SPACE * 3 * SCREENWIDTH / WIDTHSIXP) / 2.0));
    flowL.sectionInset = UIEdgeInsetsMake(SPACE * SCREENHEIGHT / HEIGHTSIXP, SPACE * SCREENWIDTH / WIDTHSIXP, SPACE * SCREENHEIGHT / HEIGHTSIXP, SPACE * SCREENWIDTH / WIDTHSIXP);
    flowL.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.hotCollectionV = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowL];
    self.hotCollectionV.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    self.hotCollectionV.delegate = self;
    self.hotCollectionV.dataSource = self;
    self.hotCollectionV.bounces = YES;
    
    [self.contentView addSubview:self.hotCollectionV];
    [_hotCollectionV release];
    
    [self.hotCollectionV registerClass:[UFOHeroTimeHotPageCollectionViewCell class] forCellWithReuseIdentifier:@"hotCell"];
    
    // 上拉加载
    self.hotCollectionV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];

    [self.hotCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(40 * SCREENHEIGHT / HEIGHTSIXP));
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
    }];
}

// 创建最新collectView
- (void)createNewCollectionView
{
    UICollectionViewFlowLayout *flowL = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowL.minimumLineSpacing = SPACE * SCREENHEIGHT / HEIGHTSIXP;
    flowL.minimumInteritemSpacing = SPACE * SCREENWIDTH / WIDTHSIXP;
    flowL.headerReferenceSize = CGSizeMake(0, 0);
    flowL.footerReferenceSize = CGSizeMake(0, 0);
    
    flowL.itemSize = CGSizeMake(((SCREENWIDTH - SPACE * 3 * SCREENWIDTH / WIDTHSIXP) / 2.0), ((SCREENWIDTH - SPACE * 3 * SCREENWIDTH / WIDTHSIXP) / 2.0));
    flowL.sectionInset = UIEdgeInsetsMake(SPACE * SCREENHEIGHT / HEIGHTSIXP, 10 * SCREENWIDTH / WIDTHSIXP, SPACE * SCREENHEIGHT / HEIGHTSIXP, SPACE * SCREENWIDTH / WIDTHSIXP);
    flowL.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.NewCollectionV = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowL];
    self.NewCollectionV.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    self.NewCollectionV.delegate = self;
    self.NewCollectionV.dataSource = self;
    self.NewCollectionV.bounces = NO;
    
    [self.contentView addSubview:self.NewCollectionV];
    [_NewCollectionV release];
    
    [self.contentView bringSubviewToFront:self.hotCollectionV];
    
    [self.NewCollectionV registerClass:[UFOHeroTimeNewPageCollectionViewCell class] forCellWithReuseIdentifier:@"newCell"];
    
    [self.NewCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(40 * SCREENHEIGHT / HEIGHTSIXP));
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
    }];
    
}

// 单元数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.hotCollectionV == collectionView) {
        if (self.hotDataArr) {
            return self.hotDataArr.count;
        }
    }
    if (self.NewCollectionV == collectionView) {
        if (self.NewDataArr) {
            return self.NewDataArr.count;
        }
    }
    return 0;
}

// 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.hotCollectionV == collectionView) {
        UFOHeroTimeHotPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotCell" forIndexPath:indexPath];
        if (self.hotDataArr) {
            UFOSXPHeroTimeHotPageListModel *model = [self.hotDataArr objectAtIndex:indexPath.row];
            cell.nameL.text = model.game_name;
            [cell.picImageV sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"zanweitu.png"]];
            cell.timeL.text = [self timeFormatted:model.share_time];
            cell.viewCountL.text = [NSString stringWithFormat:@"%@", model.video_views];
            cell.goodCountL.text = [NSString stringWithFormat:@"%@", model.video_praises];
            [cell createTypeLabelWithTypeNum:model.type_mode];
            return cell;
        }
    }
    if (self.NewCollectionV == collectionView) {
        if (self.NewDataArr) {
            UFOHeroTimeNewPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newCell" forIndexPath:indexPath];
            UFOSXPHeroTimeNewPageListModel *model = [self.NewDataArr objectAtIndex:indexPath.row];
            cell.nameL.text = model.game_name;
            [cell.picImageV sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"zanweitu.png"]];
            cell.timeL.text = [self timeFormatted:model.share_time];
            cell.viewCountL.text = [NSString stringWithFormat:@"%@", model.video_views];
            cell.goodCountL.text = [NSString stringWithFormat:@"%@", model.video_praises];
            [cell createTypeLabelWithTypeNum:model.type_mode];
            return cell;
        }
    }
    
    return nil;
    
}

// 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.hotCollectionV == collectionView) {
        [self.delegate enterPlayPageFromHotWithIndex:indexPath.item];
    }
    if (self.NewCollectionV == collectionView) {
        [self.delegate enterPlayPageFromNewWithIndex:indexPath.item];
    }
}

// segment点击方法
- (void)segmentAction:(UISegmentedControl *)sender
{
    if (0 == sender.selectedSegmentIndex) {
        [self.contentView bringSubviewToFront:self.hotCollectionV];
        self.hotCollectionV.contentOffset = CGPointMake(0, 0);
        [self.delegate getHotPageDataAgain];
    }
    if (1 == sender.selectedSegmentIndex) {
        [self.contentView bringSubviewToFront:self.NewCollectionV];
        self.NewCollectionV.contentOffset = CGPointMake(0, 0);
        [self.delegate getNewPageDataAgain];
    }
}

// 上拉重载
- (void)footerRefresh
{
    [self.delegate getMoreHotPageData];
}

// 时间戳转标准时间封装方法
- (NSString *)timeFormatted:(NSNumber *)seconds
{
    // NSNumber转成NSString
    NSString *str= [NSString stringWithFormat:@"%@", seconds];// 时间戳
    
    NSTimeInterval time = [str doubleValue];
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    
    return currentDateStr;
    
}
@end
